package main

import (
	"database/sql"
	"fmt"
	"math/rand"
	"time"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	dsn := "root:123456@tcp(127.0.0.12.9:3306)/go_project"
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		panic(err)
	}
	defer db.Close()

	if err = db.Ping(); err != nil {
		panic(err)
	}

	db.SetMaxOpenConns(5)

	stmt, err := db.Prepare("select * from user where id = ? ")
	if err != nil {
		panic(err)
	}
	defer stmt.Close()
	{
		row1 := stmt.QueryRow(10002)
		var id, val int
		row1.Scan(&id, &val)
		fmt.Println("id: ", id, "val: ", val)
	}
	{
		row1 := stmt.QueryRow(10075)
		var id, val int
		row1.Scan(&id, &val)
		fmt.Println("id:", id, "val:", val)
	}
	{
		row1 := stmt.QueryRow(10072)
		var id, val int
		row1.Scan(&id, &val)
		fmt.Println("id:", id, "val:", val)
	}
	{
		row1 := stmt.QueryRow(10043)
		var id, val int
		row1.Scan(&id, &val)
		fmt.Println("id:", id, "val:", val)
	}


	stmt0, err := db.Prepare("insert into user (id) values(?)")
	if err != nil{
		panic(err)
	}
	defer stmt0.Close()

	rand.Seed(time.Now().UnixNano())
	for i := 0; i < 10; i++ {
		stmt0.Exec(rand.Intn(10100))
	}

}
